package com.yxzr.youyou;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yxzr.youyou.adapter.HotSearchAdapter;
import com.yxzr.youyou.adapter.SearchMovieAdapter;
import com.yxzr.youyou.base.BaseActivity;
import com.yxzr.youyou.bean.HotSearchBean;
import com.yxzr.youyou.bean.SearchBean;
import com.yxzr.youyou.retrofit.BaseObserver;
import com.yxzr.youyou.retrofit.RequestUtil;
import com.yxzr.youyou.utils.NullStringToEmptyAdapterFactory;
import com.yxzr.youyou.utils.RecyclerItemDecoration;
import com.yxzr.youyou.utils.SharedPreferencesHelper;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SearchActivity extends BaseActivity {
    private EditText search_et;
    private ImageView search_img,search_img_close;
    private LinearLayout search_linear_hot;
    private RecyclerView search_movie;
    private RecyclerView search_rv;
    private SharedPreferencesHelper sp;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        sp = new SharedPreferencesHelper(this,"logindata");
        initView();
    }

    private void initView() {
        search_et = findViewById(R.id.search_et);
        search_img = findViewById(R.id.search_img);
        search_linear_hot = findViewById(R.id.search_linear_hot);
        search_img_close = findViewById(R.id.search_img_close);
        search_movie = findViewById(R.id.search_movie);
        search_rv = findViewById(R.id.search_rv);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        search_movie.setLayoutManager(linearLayoutManager);
        search_movie.addItemDecoration(new RecyclerItemDecoration(20,1));

        GridLayoutManager layoutManager = new GridLayoutManager(this,3);
        search_rv.setLayoutManager(layoutManager);
        search_rv.addItemDecoration(new RecyclerItemDecoration(10,3));

        search_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                search_linear_hot.setVisibility(View.GONE);
                search_rv.setVisibility(View.VISIBLE);
                search();
            }
        });
        search_img_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(search_et.getText())){
                    finish();
                }else {
                    search_linear_hot.setVisibility(View.VISIBLE);
                    search_rv.setVisibility(View.GONE);
                    search_et.setText("");
                }
            }
        });
        initData();
    }


    private void initData(){
        RequestUtil.getInstence().API().hotSearch()
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        HotSearchBean bean = gson.fromJson(t,HotSearchBean.class);
                        search_movie.setAdapter(new HotSearchAdapter(SearchActivity.this,bean.getData()));
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }

    private void search(){
        if (TextUtils.isEmpty(search_et.getText().toString().trim())){
            Toast.makeText(SearchActivity.this,"请输入搜索内容",Toast.LENGTH_LONG).show();
            return;
        }
        RequestUtil.getInstence().API().search((String) sp.getSharedPreference("token",""),search_et.getText().toString().trim())
                .subscribeOn(Schedulers.io()) // 在子线程中进行Http访问
                .observeOn(AndroidSchedulers.mainThread()) // UI线程处理返回接口
                .subscribe(new BaseObserver(this,true){
                    @Override
                    protected void onSuccees(String t) {
                        Gson gson = new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create();
                        SearchBean bean = gson.fromJson(t,SearchBean.class);
                        search_rv.setAdapter(new SearchMovieAdapter(SearchActivity.this,bean.getData()));
                    }

                    @Override
                    protected void onFail(int error_code) {

                    }

                    @Override
                    protected void onComp() {

                    }

                    @Override
                    protected void onErr(Throwable e) {

                    }
                });
    }
}
